package com.liner.homework;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class Homework01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Random rand = new Random();
/*
        1. 猜数字
        2. 生成N位验证码
        3. 找到2到100之内的所有素数
        4. 飞机票打折
        5. 评委打分:去掉最高分和最低分之后的平均分
*/

        //1. 猜数字
        System.out.println("系统随机数字中...");
        int random = rand.nextInt(100);
        while (true){
            System.out.print("请输入所猜数字：");
            int num = scanner.nextInt();
            if (num == random){
                System.out.println("恭喜你猜对了");
                break;
            }else if (num > random){
                System.out.println("大了");
            }else {
                System.out.println("小了");
            }

        }

        //2. 生成N位验证码
        System.out.print("输入想要生成几位数验证码:");
        int num = scanner.nextInt();
        String code = verCode(num);
        System.out.println(code);


        //3. 找到2到100之内的所有素数
        System.out.println("输出2~100之间的素数:");
        for (int i = 2; i <=100 ; i++) {
            boolean flag = true;
            prime(i, flag);
        }
        //4. 飞机票打折
        System.out.print("请输入飞机票单价:");
        int price = scanner.nextInt();
        System.out.print("请输入当前月份:");
        int month = scanner.nextInt();
        System.out.print("请输入预购舱位（1.头等舱2.商务舱3.经济舱）:");
        int type = scanner.nextInt();

        double calFinalPrice = calFinalPrice(price, month, type);
        if (calFinalPrice != -1){
            System.out.println("最终花费:" + calFinalPrice);
        }

        //5. 评委打分:去掉最高分和最低分之后的平均分
        System.out.print("请输入本场有几位评委:");
        int count = scanner.nextInt();
        double[] scores = inputData(count);

        double avg = calAvg(scores);
        System.out.println("本场平均分为：" + avg);


    }

    /**
     * 求平均数
     * @param scores
     * @return
     */
    private static double calAvg(double[] scores) {
        double sum = calTotal(scores);
        //算出平均分
        return sum / (scores.length - 2);
    }

    /**
     * 求总分
     * @param scores
     * @return
     */
    private static double calTotal(double[] scores) {
        double sum = 0;
        //数组升序排序
        Arrays.sort(scores);
        //取出去掉一个最高分去掉一个最低分
        for (int i = 1; i < scores.length - 1; i++) {
            sum += scores[i];
        }
        return sum;
    }

    /**
     * 本场评委分数
     * @param count
     * @return
     */
    private static double[] inputData(int count) {
        Scanner scanner = new Scanner(System.in);
        double[] scores = new double[count];
        for (int i = 0; i < count; i++) {
            System.out.print("第" + (i + 1) + "位评委的打分为:");
            scores[i] = scanner.nextDouble();
            if (scores[i] < 0 || scores[i] > 100) {
                System.out.println("请输入0~100区间的分数");
                break;
            }
        }
        return scores;
    }

    /**
     * 飞机票打折
     * @param price
     * @param month
     * @param type
     */
    private static double calFinalPrice(int price, int month, int type) {
        double finalPrice;
        if (price < 0) {
            System.out.println("请输入正确价格");
            return -1;
        }
        if (month < 1 || month > 12) {
            System.out.println("请输入正确月份");
            return -1;
        }
        if (type < 1 || type > 3) {
            System.out.println("请输入正确舱位");
            return -1;
        }

        if (month >= 5 && month <= 10) {
            //旺季
            switch (type) {
                case 1:
                    finalPrice = price * 0.9;
                    break;
                case 2:
                    finalPrice = price * 0.85;
                    break;
                default:
                    finalPrice = price * 0.8;
            }

        } else {
            //淡季
            switch (type) {
                case 1:
                    finalPrice = price * 0.7;
                    break;
                case 2:
                    finalPrice = price * 0.65;
                    break;
                default:
                    finalPrice = price * 0.6;
            }
        }
        return finalPrice;

    }


    /**
     * 生成N位验证码
     */
    public static String verCode(int len) {
        String code = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrlstuvwxyz";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < len; i++) {
            int index = (int) (Math.random() * code.length());
            sb.append(code.charAt(index));
        }
        return sb.toString();
    }



    /**
     * 判断是否为素数
     * @param number
     * @param flag
     */
    private static void prime(int number, boolean flag) {
        for (int i = 2; i <= number /2 ; i++) {
            if (number % i == 0){
                flag = false;
                break;
            }
        }
        if (flag){
            System.out.print(number + "\t");
        }

    }


}
